package com.kust.smarthome.web.controller;

import com.kust.smarthome.web.common.exception.XRKException;
import com.kust.smarthome.web.dto.UserDto;
import com.kust.smarthome.web.model.PersonDetails;
import com.kust.smarthome.web.model.Relation;
import com.kust.smarthome.web.model.User;
import com.kust.smarthome.web.service.IPersonDetailsService;
import com.kust.smarthome.web.service.IRelationService;
import com.kust.smarthome.web.service.IUserService;
import com.kust.smarthome.web.vo.RelationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.security.Principal;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/user")
@Api(value = "用户相关操作", tags = {"user-tag"})
public class  UserController {

    @Resource
    private IUserService userService;

    @Resource
    private IRelationService relationService;

    @Resource
    private IPersonDetailsService personDetailsService;
    
    @GetMapping("/addRelationAndPersonDetails")
    @ApiOperation(value = "添加关系人", notes = "用户提供关系人基本信息添加关系人")
    public void addRelation(Principal principal, RelationVo relationVo) {

        User user = userService.getByUsername(principal.getName());

//        Relation relation = new Relation()
//                .setUserId(user.getId())
//                .setRelationId(relationVo.getRelationShip())
//                .setCreated(LocalDateTime.now());
//        int relationId = relationService.insert(relation);

        PersonDetails personDetails = new PersonDetails()
                .setUserId(user.getId())
                .setRelationId(1)
                .setLogoId(relationVo.getLogoId())
                .setSafeDistance(relationVo.getSafeDistance())
                .setPersonName(relationVo.getPersonName())
                .setPersonBirth(relationVo.getPersonBirth())
//                .setPersonSex(relationVo.getPersonSex())
                .setHeight(relationVo.getHeight())
                .setWeight(relationVo.getWeight());
        personDetailsService.insert(personDetails);

    }

    @GetMapping("/deleteRelation")
    @ApiOperation("删除关系人")
    public boolean deleteRelation(Principal principal, Integer relationId) throws XRKException {

        User user = userService.getByUsername(principal.getName());

        if (user == null)
            throw new XRKException("用户不能为空");

        return relationService.removeRelation(user.getId(), relationId);
    }

    @GetMapping("/relationList")
    @ApiOperation("用户所有的关系人信息")
    public List<Relation> relationList(Principal principal) {
        User user = userService.getByUsername(principal.getName());
        return relationService.relationListByUser(user.getId());
    }

    @GetMapping("/detailList")
    @ApiOperation("所有家人详细信息")
    public List<PersonDetails> findPersonList(Principal principal){
        User user = userService.getByUsername(principal.getName());
        return personDetailsService.personDetailByUser(user.getId());
    }

    @GetMapping("/getUserInfo")
    @ApiOperation("获取用户信息")
    public UserDto getUserInfo(Principal principal) {
        User user = userService.getByUsername(principal.getName());

        return new UserDto()
                .setNickname(user.getNickname())
                .setId(user.getId())
                .setPortrait(user.getPortrait());
    }

    @PostMapping("/upload")
    @ApiOperation("上传用户头像(修改)")
    public boolean uploadLogo(Principal principal, String url) {
        User user = userService.getByUsername(principal.getName());
        user.setPortrait(url);
        return userService.updateById(user);
    }

}
